# Pre-lab 3

In Lab 1 you were introduced to the field of engineering robotics and the Arduino programming basics you will need for the robotics tasks you will see later in the course. In Lab 2 you learned some hardware basics, explored the Arduino IDE, and wired up your robots.

To prepare for Lab 3, you are asked to do some background research on the servo motors and the HC-SR04 ultrasonic sensor of the Arduino robot. The goal is to understand how the servos move the robot and how the sensor works so we can begin using it for control.

# Homework

# Servos

  1. Do the second exercise in the Robot Lab: Move a Robot. Provide your solution and explain how the servos are being controlled to complete the task.

# HC-SR04 ultrasonic sensor

  1. Do some background research on the HC-SR04 ultrasonic sensor using these tutorials:

or some other source and answer the following questions.

a) In general terms, how does the HC-SR04 ultrasonic sensor work?

b) Describe the purpose of each pin on the HC-SR04 ultrasonic sensor.

  1. Study the boilerplate code for the HC-SR04 ultrasonic sensor below. Using the tutorials and the Arduino language reference provided with the Lab 1 notes or some other source answer the questions that follow.
// defines Arduino pin numbers

const int trigPin = 12;
const int echoPin = 11;

// defines variables

long duration;
int distance;

void setup() 
{
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}

void loop() 
{

// Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

// Calculating the distance
  distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
  Serial.print("Distance from the object = ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(1000);
}

a) What is the significance of using const to define Arduino pin numbers?

b) What is the long data type? How many different data types are given in the Arduino language reference?

c) Explain how the time between a triggered sound wave pulse and its received echo is used to determine distance. Specifically, explain how you get this equation in the code:

distance= duration*0.034/2;

d) What is serial communication? What is it being used for in this sketch (program)?

Submit your answers in a single file as either a word or text document by the due date using the Omnivox course management system.